package wh.搜索查找位置;

public class Solution {
    public static void main(String[] args) {
        int[] arr1 = {1,2,3,4,5,6};
        searchInsert(arr1,2);
    }


    public static int searchInsert(int[] nums , int target){
       int start = 0;
       int end = nums.length - 1;
       int middle = 0;
       while (start <= end){
           //因为定义了middle类型为int行，所以不必担心middle会为小数.(而且start和end都为整数，除以2肯定是整数，除以2.0才可能为小数)
           middle = (start + end) / 2;
           if(target == nums[middle]){
               return middle;
           }else if(target > nums[middle]){
               start = middle + 1;
           }else {
               end = middle - 1;
           }
       }
       return start;
    }
}
